package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.SysDictData;
import com.cloud.system.service.ISysDictDataService;
import com.cloud.system.service.ISysDictTypeService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author zx, yzj
 */
@Tag(name = "数据字典")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Operation(summary = "查询字典列表")
    @RequestPermissions("system:dict:list")
    @GetMapping("/list")
    public Result list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Operation(summary = "导出字典信息")
    @Logs(title = "导出字典管理", businessType = BusinessType.EXPORT)
    @RequestPermissions("system:dict:export")
    @GetMapping("/export")
    public void export(SysDictData dictData) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        String titleName = "字典列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_dict_data", SysDictData.class,
                titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "查询字典详细")
    @Parameter(name = "dictCode", description = "字典编码")
    @RequestPermissions("system:dict:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long dictCode) {
        return Result.success("查询成功", dictDataService.selectDictDataById(dictCode));
    }

    @Operation(summary = "根据类型查询信息")
    @Parameter(name = "dictType", description = "字典类型")
    @GetMapping("/getDictType")
    public Result getDictType(String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        return Result.success("查询成功", data);
    }

    @Operation(summary = "新增字典类型")
    @Logs(title = "新增字典管理", businessType = BusinessType.INSERT)
    @RequestPermissions("system:dict:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysDictData dict) {
        return toResult(dictDataService.insertDictData(dict));
    }

    @Operation(summary = "修改字典类型")
    @Logs(title = "修改字典管理", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:dict:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysDictData dict) {
        return toResult(dictDataService.updateDictData(dict));
    }

    @Operation(summary = "删除字典类型")
    @Parameter(name = "dictCodes", description = "字典编码[多个用,隔开]")
    @Logs(title = "删除字典管理", businessType = BusinessType.DELETE)
    @RequestPermissions("system:dict:remove")
    @PostMapping("/remove")
    public Result remove(String dictCodes) {
        List<Long> list = ToolUtils.setIdsToList(dictCodes);
        return toResult(dictDataService.deleteDictDataByIds(list));
    }

}
